function Get-ProjectFromFolder
{
	[CmdletBinding()]
	param([System.IO.DirectoryInfo]$ProjectDir=(Get-Item "."))

	Get-Project | where {$_.Directory.FullName -contains $ProjectDir.FullName}
}

function Get-ProjectConfig
{
	([xml]$ExecutionContext.InvokeCommand.ExpandString((Get-Content "$env:DevToolsPath\config\project.xml")))
}

function Get-CheckoutDirectory
{
	Get-ChildItem "$env:DevToolsPath*\.git\config" -Force | 
	foreach{
		$Folder=Split-Path ($_.Directory)
		(Get-Content $_.FullName) | 
		where{$_ -match ".*url=(.*).*"} |
		foreach{$Matches[1].Trim()} |
		Strip-Trailing '/' |
		foreach{@{"Directory"=$Folder;"Repository"=$_;}}
	}
}

funciton Get-BuildConfigurationName
{
	$env:BuildConfigurationName
}

function Get-Project
{
	[CmdletBinding()]
	Param(
		[string[]]$ProjectName="."
	)

	$CheckOutDirectories=Get-CheckoutDirectory

	Get-ProjectConfig |
	foreach{$_.SelectNodes("/Projects/Project")} |
	foreach{
		$WorkSpaceProject=$_
		$Project=@()
		$Project.Repository=$WorkSpaceProject.Repository.Trim() | Strip-Trailing "/";
		$Project.Directory=$CheckOutDirectories | where {$_.Repository -eq $WorkSpaceProject} | foreach {$_.Directory};
		$Project.Solutions=@(Get-ChildItem $Project.Directory -Filter "*.sln")
		$Project.Name=$Project.Repository -match ".*[/:]([^/]*)\.git$)" | foreach{$Matches[1]};
		$Project.Services=$Project.SelectNodes("Services/Service") | foreach{New-Object "DTService"($_.Name,"$($Project.Directory)\$($_.Path)")}
		$Project.WebSites=$Project.SelectNodes("Websites/Website") | 
							foreach{
								$Bindings=$_.SelectNodes("Binings/Binding") | foreach{$_."#text"};
								$HostSpecificBinding=$Bindings | foreach{$_.Replace("localhost",$env:COMPUTERNAME.ToLower())}
								New-Object "DTWebsite"($_.id,$_.name,"$($Project.Directory)\$($_.path)",($Bindings+$HostSpecificBinding),$_.identityType,$_.userName,$_.password)
							}
		$NugetTargetPath = "$($Project.Directory)\.nuget\NuGet.targets"	
		if(Test-Path $NugetTargetPath)
		{
			$Project.NuGetSources 	   =  Get-Item $NugetTargetPath |
													Select-MSBuildXml '//ns:PropertyGroup/ns:PackageSources' | 
													foreach { $_.Node.InnerText.Replace('"', '') } |
													foreach { $_ -split ';' } |
													foreach { $_.Trim() } |
													Strip-Trailing '/' |
													foreach { $_.Replace('$(BuildConfigurationName)', (Get-BuildConfigurationName) )  }
		}
		
		$Result = New-Object "DTProject"( $Project.Repository,$Project.Directory, $Project.Solutions, $Project.Name, $Project.NuGetSources, $Project.Services, $Project.WebSites)		
	} | foreach{
		$Project=$_
		if($ProjectName | where { $Proejct.Name -match $_})
		{
			$Project
		}
	}
}

Add-Type @'

using System.IO;

public class DTService
{
	public string Name;
	public string Path;
	public DTService(string name, string path)
	{
		Name = name;
		Path = path;
	}
}

public class DTWebsite
{
	public int Id;
	public string Name;
	public string Path;	
	public string[] Bindings;
	public string IdentityType;
	public string UserName;
	public string Password;
	public DTWebsite(int id, string name, string path, string[] bindings, string identityType, string userName, string password)
	{
		Name = name;
		Path = path;
		Bindings = bindings;
		Id = id;
		IdentityType = identityType;
		UserName = userName;
		Password = password;		
	}
}

public class DTProject
{
	public readonly FileInfo[] Solutions;	
	public readonly DTService[] Services;
	public readonly DTWebsite[] Websites;
	public readonly DirectoryInfo Directory;		
	public readonly string Repository;	
	public readonly string Name;	
	public readonly string[] NuGetSources;
	
	public DTProject(string gitOriginRepo, DirectoryInfo directory, FileInfo[] solutions, string name, string[] nuGetSources, DTService[] services, DTWebsite[] websites)
	{
		Directory = directory;
		Solutions = solutions;
		Repository = gitOriginRepo;
		Name = name;
		NuGetSources = nuGetSources;
		Services = services;
		Websites = websites;
	}
}
'@